package com.nd.qa.shareplate.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nd.qa.shareplate.IDao.LoginMapper;
import com.nd.qa.shareplate.common.CPResultCode;
import com.nd.qa.shareplate.model.User;
import com.nd.qa.shareplate.service.LoginService;

@Service("com.nd.qa.shareplate.service.LoginService")
public class LoginServiceImpl implements LoginService{

	@Autowired
	private LoginMapper loginDao;
	//@Autowired
	//private UserRoleMapper userRoleMapper;
	/*@Autowired
	@Qualifier("ehCacheManager")
	EhCacheCacheManager ehCacheManager;//需要引入spring-context-support-3.2.2.jar
*/	
	@Override
	public HashMap<String, Object> queryUserById(HashMap<String, Object> param) throws Exception{
	
		HashMap<String, Object> returnMap = loginDao.queryUserById(param);
		HashMap<String, Object> res = new HashMap<String,Object>();
		if (returnMap!=null) {
			String userPwd = String.valueOf(param.get("userPassword"));
			String userPassword = String.valueOf(returnMap.get("user_password"));
			if (userPwd.equals(userPassword)) {
				res.put("code", CPResultCode.CP0000);
				res.put("user_id", String.valueOf(returnMap.get("user_id")));
			}else {
				res.put("code", CPResultCode.CP1001);
				res.put("message", "密码或者账户错误");
			}
		}else{
			res.put("code", CPResultCode.CP1001);
			res.put("message", "对不起，该账户不存在");
		};
		return res;
	}

	@Override
	public List<HashMap<String, Object>> queryHeaderMenuFunction() throws Exception{
		
		/*Cache cache = this.ehCacheManager.getCache("headerInfo");
		StringBuffer cacheKey = new StringBuffer("qa-header-info");
		if (!CollectionUtils.isEmpty(dataBusMap)) {
			Iterator<Entry<String, Object>> iter = dataBusMap.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iter.next();
				Object val = entry.getValue();
				cacheKey.append("_");
				cacheKey.append(val);
			}
		}
		if (cache.get(cacheKey.toString()) != null) {
			menuList = (List<HashMap<String, Object>>) cache.get(cacheKey.toString()).get();
		}
		if (!CollectionUtils.isEmpty(menuList)) {
			return menuList;
		}
		//存放在缓存中
	    cache.put(cacheKey.toString(), menuList);*/
		List<HashMap<String, Object>> menuList = loginDao.queryHeaderMenuFunction();
		return menuList;
	}

	@Override
	public List<HashMap<String, Object>> queryUserRoleById(HashMap<String, Object> map) throws Exception {
		return loginDao.queryUserRoleById(map);
	}

	/*@Override
	public List<HashMap<String, Object>> queryUserAuthority(String userId) throws Exception {
		return loginMapper.queryUserAuthority(userId);
	}*/

}
